package com.itheima2;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class MyLocalDateTimeUtil1 {
    private MyLocalDateTimeUtil1(){

    }
    // 按照默认的模式“yyyy-MM-dd HH:mm:ss”将日期对象格式化为字符串
    public static String localDateTimeToString(LocalDateTime dateTime){
        return localDateTimeToString(dateTime,null);
    }
    // 按指定的模式将日期对象格式化为字符串
    public static String localDateTimeToString(LocalDateTime dateTime,String patten){
        if(patten==null||" ".equals(patten)){
            patten="yyyy-MM-dd HH:mm:ss";
        }
        // 日期格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patten);
        String str = formatter.format(dateTime);
        return str;
    }
    // 按照指定的模式将字符串解析为日期对象
    public static LocalDateTime localToStringDateTime( String strDays){
        return localToStringDateTime(strDays,null);
    }
    public static LocalDateTime localToStringDateTime(String strDays,String patten){
        if (patten==null||" ".equals(patten)){
            patten="yyyy-MM-dd HH:mm：ss";
        }
        DateTimeFormatter date = DateTimeFormatter.ofPattern(patten);
        LocalDateTime parse = LocalDateTime.parse(strDays,date);
        return parse;
    }
    public static long millisecondBetween(LocalDateTime dateTime1,LocalDateTime dateTime2){
        Duration between=Duration.between(dateTime1,dateTime2);
        long millis = between.toMillis();
        return millis;
    }
}
